{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "4embtkV0pNxM"
   },
   "source": [
    "Deep Learning\n",
    "=============\n",
    "\n",
    "Assignment 4\n",
    "------------\n",
    "\n",
    "Previously in `2_fullyconnected.ipynb` and `3_regularization.ipynb`, we trained fully connected networks to classify [notMNIST](http://yaroslavvb.blogspot.com/2011/09/notmnist-dataset.html) characters.\n",
    "\n",
    "The goal of this assignment is make the neural network convolutional."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "collapsed": true,
    "id": "tm2CQN_Cpwj0"
   },
   "outputs": [],
   "source": [
    "# These are all the modules we'll be using later. Make sure you can import them\n",
    "# before proceeding further.\n",
    "from __future__ import print_function\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from six.moves import cPickle as pickle\n",
    "from six.moves import range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "output_extras": [
      {
       "item_id": 1
      }
     ]
    },
    "colab_type": "code",
    "collapsed": false,
    "executionInfo": {
     "elapsed": 11948,
     "status": "ok",
     "timestamp": 1446658914837,
     "user": {
      "color": "",
      "displayName": "",
      "isAnonymous": false,
      "isMe": true,
      "permissionId": "",
      "photoUrl": "",
      "sessionId": "0",
      "userId": ""
     },
     "user_tz": 480
    },
    "id": "y3-cj1bpmuxc",
    "outputId": "016b1a51-0290-4b08-efdb-8c95ffc3cd01"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training set (200000, 28, 28) (200000,)\n",
      "Validation set (10000, 28, 28) (10000,)\n",
      "Test set (10000, 28, 28) (10000,)\n"
     ]
    }
   ],
   "source": [
    "pickle_file = 'notMNIST.pickle'\n",
    "\n",
    "with open(pickle_file, 'rb') as f:\n",
    "  save = pickle.load(f)\n",
    "  train_dataset = save['train_dataset']\n",
    "  train_labels = save['train_labels']\n",
    "  valid_dataset = save['valid_dataset']\n",
    "  valid_labels = save['valid_labels']\n",
    "  test_dataset = save['test_dataset']\n",
    "  test_labels = save['test_labels']\n",
    "  del save  # hint to help gc free up memory\n",
    "  print('Training set', train_dataset.shape, train_labels.shape)\n",
    "  print('Validation set', valid_dataset.shape, valid_labels.shape)\n",
    "  print('Test set', test_dataset.shape, test_labels.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "L7aHrm6nGDMB"
   },
   "source": [
    "Reformat into a TensorFlow-friendly shape:\n",
    "- convolutions need the image data formatted as a cube (width by height by #channels)\n",
    "- labels as float 1-hot encodings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "output_extras": [
      {
       "item_id": 1
      }
     ]
    },
    "colab_type": "code",
    "collapsed": false,
    "executionInfo": {
     "elapsed": 11952,
     "status": "ok",
     "timestamp": 1446658914857,
     "user": {
      "color": "",
      "displayName": "",
      "isAnonymous": false,
      "isMe": true,
      "permissionId": "",
      "photoUrl": "",
      "sessionId": "0",
      "userId": ""
     },
     "user_tz": 480
    },
    "id": "IRSyYiIIGIzS",
    "outputId": "650a208c-8359-4852-f4f5-8bf10e80ef6c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training set (200000, 28, 28, 1) (200000, 10)\n",
      "Validation set (10000, 28, 28, 1) (10000, 10)\n",
      "Test set (10000, 28, 28, 1) (10000, 10)\n"
     ]
    }
   ],
   "source": [
    "image_size = 28\n",
    "num_labels = 10\n",
    "num_channels = 1 # grayscale\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "def reformat(dataset, labels):\n",
    "  dataset = dataset.reshape(\n",
    "    (-1, image_size, image_size, num_channels)).astype(np.float32)\n",
    "  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)\n",
    "  return dataset, labels\n",
    "train_dataset, train_labels = reformat(train_dataset, train_labels)\n",
    "valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)\n",
    "test_dataset, test_labels = reformat(test_dataset, test_labels)\n",
    "print('Training set', train_dataset.shape, train_labels.shape)\n",
    "print('Validation set', valid_dataset.shape, valid_labels.shape)\n",
    "print('Test set', test_dataset.shape, test_labels.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "collapsed": true,
    "id": "AgQDIREv02p1"
   },
   "outputs": [],
   "source": [
    "def accuracy(predictions, labels):\n",
    "  return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))\n",
    "          / predictions.shape[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5rhgjmROXu2O"
   },
   "source": [
    "Let's build a small network with two convolutional layers, followed by one fully connected layer. Convolutional networks are more expensive computationally, so we'll limit its depth and number of fully connected nodes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "collapsed": true,
    "id": "IZYv70SvvOan"
   },
   "outputs": [],
   "source": [
    "batch_size = 16\n",
    "patch_size = 5\n",
    "depth = 16\n",
    "num_hidden = 64\n",
    "\n",
    "graph = tf.Graph()\n",
    "\n",
    "with graph.as_default():\n",
    "\n",
    "  # Input data.\n",
    "  tf_train_dataset = tf.placeholder(\n",
    "    tf.float32, shape=(batch_size, image_size, image_size, num_channels))\n",
    "  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))\n",
    "  tf_valid_dataset = tf.constant(valid_dataset)\n",
    "  tf_test_dataset = tf.constant(test_dataset)\n",
    "  \n",
    "  # Variables.\n",
    "  layer1_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, num_channels, depth], stddev=0.1))\n",
    "  layer1_biases = tf.Variable(tf.zeros([depth]))\n",
    "  layer2_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, depth, depth], stddev=0.1))\n",
    "  layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))\n",
    "  layer3_weights = tf.Variable(tf.truncated_normal(\n",
    "      [image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1))\n",
    "  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))\n",
    "  layer4_weights = tf.Variable(tf.truncated_normal(\n",
    "      [num_hidden, num_labels], stddev=0.1))\n",
    "  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))\n",
    "  \n",
    "  # Model.\n",
    "  def model(data):\n",
    "    conv = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')\n",
    "    hidden = tf.nn.relu(conv + layer1_biases)\n",
    "    conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')\n",
    "    hidden = tf.nn.relu(conv + layer2_biases)\n",
    "    shape = hidden.get_shape().as_list()\n",
    "    reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])\n",
    "    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)\n",
    "    return tf.matmul(hidden, layer4_weights) + layer4_biases\n",
    "  \n",
    "  # Training computation.\n",
    "  logits = model(tf_train_dataset)\n",
    "  loss = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n",
    "    \n",
    "  # Optimizer.\n",
    "  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)\n",
    "  \n",
    "  # Predictions for the training, validation, and test data.\n",
    "  train_prediction = tf.nn.softmax(logits)\n",
    "  valid_prediction = tf.nn.softmax(model(tf_valid_dataset))\n",
    "  test_prediction = tf.nn.softmax(model(tf_test_dataset))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cellView": "both",
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "output_extras": [
      {
       "item_id": 37
      }
     ]
    },
    "colab_type": "code",
    "collapsed": false,
    "executionInfo": {
     "elapsed": 63292,
     "status": "ok",
     "timestamp": 1446658966251,
     "user": {
      "color": "",
      "displayName": "",
      "isAnonymous": false,
      "isMe": true,
      "permissionId": "",
      "photoUrl": "",
      "sessionId": "0",
      "userId": ""
     },
     "user_tz": 480
    },
    "id": "noKFb2UovVFR",
    "outputId": "28941338-2ef9-4088-8bd1-44295661e628"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized\n",
      "Minibatch loss at step 0: 3.499307\n",
      "Minibatch accuracy: 6.2%\n",
      "Validation accuracy: 10.0%\n",
      "Minibatch loss at step 50: 2.228391\n",
      "Minibatch accuracy: 6.2%\n",
      "Validation accuracy: 27.1%\n",
      "Minibatch loss at step 100: 1.446163\n",
      "Minibatch accuracy: 37.5%\n",
      "Validation accuracy: 49.3%\n",
      "Minibatch loss at step 150: 0.524155\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 72.0%\n",
      "Minibatch loss at step 200: 0.801739\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 76.2%\n",
      "Minibatch loss at step 250: 1.147921\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 76.6%\n",
      "Minibatch loss at step 300: 0.449470\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 78.0%\n",
      "Minibatch loss at step 350: 0.554659\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 76.3%\n",
      "Minibatch loss at step 400: 0.248710\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 77.6%\n",
      "Minibatch loss at step 450: 0.858000\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 79.1%\n",
      "Minibatch loss at step 500: 0.821578\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 79.5%\n",
      "Minibatch loss at step 550: 0.975381\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 79.4%\n",
      "Minibatch loss at step 600: 0.310249\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 650: 0.761372\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.1%\n",
      "Minibatch loss at step 700: 0.902964\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 81.5%\n",
      "Minibatch loss at step 750: 0.069972\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 82.0%\n",
      "Minibatch loss at step 800: 0.619308\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.0%\n",
      "Minibatch loss at step 850: 0.999329\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 80.9%\n",
      "Minibatch loss at step 900: 0.755689\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.4%\n",
      "Minibatch loss at step 950: 0.544976\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.3%\n",
      "Minibatch loss at step 1000: 0.527818\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.3%\n",
      "Test accuracy: 89.6%\n"
     ]
    }
   ],
   "source": [
    "num_steps = 1001\n",
    "\n",
    "with tf.Session(graph=graph) as session:\n",
    "  tf.initialize_all_variables().run()\n",
    "  print('Initialized')\n",
    "  for step in range(num_steps):\n",
    "    offset = (step * batch_size) % (train_labels.shape[0] - batch_size)\n",
    "    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]\n",
    "    batch_labels = train_labels[offset:(offset + batch_size), :]\n",
    "    feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}\n",
    "    _, l, predictions = session.run(\n",
    "      [optimizer, loss, train_prediction], feed_dict=feed_dict)\n",
    "    if (step % 50 == 0):\n",
    "      print('Minibatch loss at step %d: %f' % (step, l))\n",
    "      print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))\n",
    "      print('Validation accuracy: %.1f%%' % accuracy(\n",
    "        valid_prediction.eval(), valid_labels))\n",
    "  print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "KedKkn4EutIK"
   },
   "source": [
    "---\n",
    "Problem 1\n",
    "---------\n",
    "\n",
    "The convolutional model above uses convolutions with stride 2 to reduce the dimensionality. Replace the strides by a max pooling operation (`nn.max_pool()`) of stride 2 and kernel size 2.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "batch_size = 16\n",
    "patch_size = 5\n",
    "depth = 16\n",
    "num_hidden = 64\n",
    "\n",
    "graph = tf.Graph()\n",
    "\n",
    "with graph.as_default():\n",
    "\n",
    "  # Input data.\n",
    "  tf_train_dataset = tf.placeholder(\n",
    "    tf.float32, shape=(batch_size, image_size, image_size, num_channels))\n",
    "  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))\n",
    "  tf_valid_dataset = tf.constant(valid_dataset)\n",
    "  tf_test_dataset = tf.constant(test_dataset)\n",
    "  \n",
    "  # Variables.\n",
    "  layer1_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, num_channels, depth], stddev=0.1))\n",
    "  layer1_biases = tf.Variable(tf.zeros([depth]))\n",
    "  layer2_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, depth, depth], stddev=0.1))\n",
    "  layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))\n",
    "  layer3_weights = tf.Variable(tf.truncated_normal(\n",
    "      [image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1))\n",
    "  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))\n",
    "  layer4_weights = tf.Variable(tf.truncated_normal(\n",
    "      [num_hidden, num_labels], stddev=0.1))\n",
    "  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))\n",
    "  \n",
    "  # Model.\n",
    "  def model(data):\n",
    "    conv1 = tf.nn.conv2d(data, layer1_weights, [1, 1, 1, 1], padding='SAME')\n",
    "    bias1 = tf.nn.relu(conv1 + layer1_biases)\n",
    "    pool1 = tf.nn.max_pool(bias1, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')\n",
    "    conv2 = tf.nn.conv2d(pool1, layer2_weights, [1, 1, 1, 1], padding='SAME')\n",
    "    bias2 = tf.nn.relu(conv2 + layer2_biases)\n",
    "    pool2 = tf.nn.max_pool(bias2, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')\n",
    "    shape = pool2.get_shape().as_list()\n",
    "    reshape = tf.reshape(pool2, [shape[0], shape[1] * shape[2] * shape[3]])\n",
    "    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)\n",
    "    return tf.matmul(hidden, layer4_weights) + layer4_biases\n",
    "  \n",
    "  # Training computation.\n",
    "  logits = model(tf_train_dataset)\n",
    "  loss = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n",
    "    \n",
    "  # Optimizer.\n",
    "  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)\n",
    "  \n",
    "  # Predictions for the training, validation, and test data.\n",
    "  train_prediction = tf.nn.softmax(logits)\n",
    "  valid_prediction = tf.nn.softmax(model(tf_valid_dataset))\n",
    "  test_prediction = tf.nn.softmax(model(tf_test_dataset))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized\n",
      "Minibatch loss at step 0: 4.089417\n",
      "Minibatch accuracy: 6.2%\n",
      "Validation accuracy: 10.0%\n",
      "Minibatch loss at step 50: 1.696590\n",
      "Minibatch accuracy: 37.5%\n",
      "Validation accuracy: 47.8%\n",
      "Minibatch loss at step 100: 1.180653\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 64.2%\n",
      "Minibatch loss at step 150: 0.487665\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 74.5%\n",
      "Minibatch loss at step 200: 0.836735\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 78.9%\n",
      "Minibatch loss at step 250: 1.061049\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 79.5%\n",
      "Minibatch loss at step 300: 0.417284\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 80.8%\n",
      "Minibatch loss at step 350: 0.480906\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 80.5%\n",
      "Minibatch loss at step 400: 0.222692\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 81.7%\n",
      "Minibatch loss at step 450: 0.871157\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 80.2%\n",
      "Minibatch loss at step 500: 0.670514\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.0%\n",
      "Minibatch loss at step 550: 0.816612\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 82.7%\n",
      "Minibatch loss at step 600: 0.316863\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.7%\n",
      "Minibatch loss at step 650: 0.950777\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.2%\n",
      "Minibatch loss at step 700: 0.730515\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 750: 0.025537\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 83.9%\n",
      "Minibatch loss at step 800: 0.531624\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.9%\n",
      "Minibatch loss at step 850: 0.870481\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 84.0%\n",
      "Minibatch loss at step 900: 0.597318\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 950: 0.629401\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 1000: 0.423087\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 84.8%\n",
      "Test accuracy: 91.2%\n"
     ]
    }
   ],
   "source": [
    "num_steps = 1001\n",
    "\n",
    "with tf.Session(graph=graph) as session:\n",
    "  tf.initialize_all_variables().run()\n",
    "  print('Initialized')\n",
    "  for step in range(num_steps):\n",
    "    offset = (step * batch_size) % (train_labels.shape[0] - batch_size)\n",
    "    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]\n",
    "    batch_labels = train_labels[offset:(offset + batch_size), :]\n",
    "    feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}\n",
    "    _, l, predictions = session.run(\n",
    "      [optimizer, loss, train_prediction], feed_dict=feed_dict)\n",
    "    if (step % 50 == 0):\n",
    "      print('Minibatch loss at step %d: %f' % (step, l))\n",
    "      print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))\n",
    "      print('Validation accuracy: %.1f%%' % accuracy(\n",
    "        valid_prediction.eval(), valid_labels))\n",
    "  print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "klf21gpbAgb-"
   },
   "source": [
    "---\n",
    "Problem 2\n",
    "---------\n",
    "\n",
    "Try to get the best performance you can using a convolutional net. Look for example at the classic [LeNet5](http://yann.lecun.com/exdb/lenet/) architecture, adding Dropout, and/or adding learning rate decay.\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The CNN below is loosely inspired by the LeNet5 architecture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "batch_size = 16\n",
    "patch_size = 5\n",
    "depth = 16\n",
    "num_hidden = 64\n",
    "\n",
    "graph = tf.Graph()\n",
    "\n",
    "with graph.as_default():\n",
    "\n",
    "  # Input data.\n",
    "  tf_train_dataset = tf.placeholder(\n",
    "    tf.float32, shape=(batch_size, image_size, image_size, num_channels))\n",
    "  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))\n",
    "  tf_valid_dataset = tf.constant(valid_dataset)\n",
    "  tf_test_dataset = tf.constant(test_dataset)\n",
    "  \n",
    "  # Variables.\n",
    "  layer1_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, num_channels, depth], stddev=0.1))\n",
    "  layer1_biases = tf.Variable(tf.zeros([depth]))\n",
    "  layer2_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, depth, depth], stddev=0.1))\n",
    "  layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))\n",
    "  size3 = ((image_size - patch_size + 1) // 2 - patch_size + 1) // 2\n",
    "  layer3_weights = tf.Variable(tf.truncated_normal(\n",
    "      [size3 * size3 * depth, num_hidden], stddev=0.1))\n",
    "  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))\n",
    "  layer4_weights = tf.Variable(tf.truncated_normal(\n",
    "      [num_hidden, num_labels], stddev=0.1))\n",
    "  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))\n",
    "  \n",
    "  # Model.\n",
    "  def model(data):\n",
    "    # C1 input 28 x 28\n",
    "    conv1 = tf.nn.conv2d(data, layer1_weights, [1, 1, 1, 1], padding='VALID')\n",
    "    bias1 = tf.nn.relu(conv1 + layer1_biases)\n",
    "    # S2 input 24 x 24\n",
    "    pool2 = tf.nn.avg_pool(bias1, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')\n",
    "    # C3 input 12 x 12\n",
    "    conv3 = tf.nn.conv2d(pool2, layer2_weights, [1, 1, 1, 1], padding='VALID')\n",
    "    bias3 = tf.nn.relu(conv3 + layer2_biases)\n",
    "    # S4 input 8 x 8\n",
    "    pool4 = tf.nn.avg_pool(bias3, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')\n",
    "    # F6 input 4 x 4\n",
    "    shape = pool4.get_shape().as_list()\n",
    "    reshape = tf.reshape(pool4, [shape[0], shape[1] * shape[2] * shape[3]])\n",
    "    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)\n",
    "    return tf.matmul(hidden, layer4_weights) + layer4_biases\n",
    "  \n",
    "  # Training computation.\n",
    "  logits = model(tf_train_dataset)\n",
    "  loss = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n",
    "    \n",
    "  # Optimizer.\n",
    "  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)\n",
    "  \n",
    "  # Predictions for the training, validation, and test data.\n",
    "  train_prediction = tf.nn.softmax(logits)\n",
    "  valid_prediction = tf.nn.softmax(model(tf_valid_dataset))\n",
    "  test_prediction = tf.nn.softmax(model(tf_test_dataset))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized\n",
      "Minibatch loss at step 0: 2.432403\n",
      "Minibatch accuracy: 18.8%\n",
      "Validation accuracy: 11.3%\n",
      "Minibatch loss at step 50: 1.741629\n",
      "Minibatch accuracy: 43.8%\n",
      "Validation accuracy: 46.3%\n",
      "Minibatch loss at step 100: 1.296582\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 62.8%\n",
      "Minibatch loss at step 150: 0.774304\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 69.6%\n",
      "Minibatch loss at step 200: 1.033114\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 70.8%\n",
      "Minibatch loss at step 250: 1.244565\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 75.0%\n",
      "Minibatch loss at step 300: 0.575214\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 76.5%\n",
      "Minibatch loss at step 350: 0.622662\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 75.1%\n",
      "Minibatch loss at step 400: 0.430026\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 77.6%\n",
      "Minibatch loss at step 450: 0.963384\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 76.3%\n",
      "Minibatch loss at step 500: 0.817511\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 78.4%\n",
      "Minibatch loss at step 550: 1.014760\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 77.6%\n",
      "Minibatch loss at step 600: 0.400438\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 79.5%\n",
      "Minibatch loss at step 650: 0.850994\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 79.6%\n",
      "Minibatch loss at step 700: 1.141740\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 79.8%\n",
      "Minibatch loss at step 750: 0.094994\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 80.1%\n",
      "Minibatch loss at step 800: 0.603943\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 80.5%\n",
      "Minibatch loss at step 850: 1.054630\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 80.2%\n",
      "Minibatch loss at step 900: 0.720748\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 80.6%\n",
      "Minibatch loss at step 950: 0.647146\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.3%\n",
      "Minibatch loss at step 1000: 0.416097\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 1050: 0.513317\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 80.7%\n",
      "Minibatch loss at step 1100: 0.579172\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.1%\n",
      "Minibatch loss at step 1150: 0.377961\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 81.7%\n",
      "Minibatch loss at step 1200: 1.004240\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 82.5%\n",
      "Minibatch loss at step 1250: 0.658937\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.5%\n",
      "Minibatch loss at step 1300: 0.350493\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 81.8%\n",
      "Minibatch loss at step 1350: 1.012370\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 80.6%\n",
      "Minibatch loss at step 1400: 0.424690\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 82.5%\n",
      "Minibatch loss at step 1450: 0.393176\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.9%\n",
      "Minibatch loss at step 1500: 0.580200\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.7%\n",
      "Minibatch loss at step 1550: 0.548097\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 83.1%\n",
      "Minibatch loss at step 1600: 1.047646\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 82.5%\n",
      "Minibatch loss at step 1650: 0.696714\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 82.9%\n",
      "Minibatch loss at step 1700: 0.766779\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.4%\n",
      "Minibatch loss at step 1750: 0.478430\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.2%\n",
      "Minibatch loss at step 1800: 0.581443\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 1850: 0.702833\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 1900: 0.347376\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 83.2%\n",
      "Minibatch loss at step 1950: 0.464471\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.2%\n",
      "Minibatch loss at step 2000: 0.141003\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 2050: 0.779174\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.4%\n",
      "Minibatch loss at step 2100: 0.219605\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 2150: 0.438449\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 2200: 0.395086\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 2250: 0.509446\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.2%\n",
      "Minibatch loss at step 2300: 0.615863\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 2350: 0.485477\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.2%\n",
      "Minibatch loss at step 2400: 0.412634\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 2450: 0.635194\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.1%\n",
      "Minibatch loss at step 2500: 0.839775\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 83.6%\n",
      "Minibatch loss at step 2550: 0.558345\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.4%\n",
      "Minibatch loss at step 2600: 0.150379\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 84.8%\n",
      "Minibatch loss at step 2650: 0.406153\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.4%\n",
      "Minibatch loss at step 2700: 0.547834\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 2750: 1.475792\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 2800: 0.547563\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 84.6%\n",
      "Minibatch loss at step 2850: 0.134402\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.1%\n",
      "Minibatch loss at step 2900: 0.340885\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.1%\n",
      "Minibatch loss at step 2950: 0.484956\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.1%\n",
      "Minibatch loss at step 3000: 0.728811\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.2%\n",
      "Minibatch loss at step 3050: 0.355967\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 3100: 0.450600\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 3150: 0.757128\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 85.3%\n",
      "Minibatch loss at step 3200: 0.619602\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.3%\n",
      "Minibatch loss at step 3250: 0.296807\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.9%\n",
      "Minibatch loss at step 3300: 0.144331\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 3350: 0.436762\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.6%\n",
      "Minibatch loss at step 3400: 0.606813\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.7%\n",
      "Minibatch loss at step 3450: 0.472831\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.0%\n",
      "Minibatch loss at step 3500: 0.314739\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.2%\n",
      "Minibatch loss at step 3550: 0.166248\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 3600: 0.194450\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.1%\n",
      "Minibatch loss at step 3650: 0.703678\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 3700: 0.913813\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 85.7%\n",
      "Minibatch loss at step 3750: 0.822634\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.6%\n",
      "Minibatch loss at step 3800: 0.008681\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 85.9%\n",
      "Minibatch loss at step 3850: 0.539536\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.4%\n",
      "Minibatch loss at step 3900: 0.520987\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.0%\n",
      "Minibatch loss at step 3950: 0.022716\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 86.0%\n",
      "Minibatch loss at step 4000: 0.401299\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.3%\n",
      "Minibatch loss at step 4050: 0.920850\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 85.9%\n",
      "Minibatch loss at step 4100: 0.394480\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.1%\n",
      "Minibatch loss at step 4150: 1.032531\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 4200: 0.337293\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.9%\n",
      "Minibatch loss at step 4250: 0.489229\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 4300: 0.625643\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 4350: 0.226855\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.3%\n",
      "Minibatch loss at step 4400: 1.245873\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 4450: 0.532430\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.1%\n",
      "Minibatch loss at step 4500: 0.533970\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.3%\n",
      "Minibatch loss at step 4550: 0.302638\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 4600: 0.546116\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.4%\n",
      "Minibatch loss at step 4650: 1.007509\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 4700: 0.324029\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 4750: 0.764786\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 4800: 0.484530\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 4850: 0.347425\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 4900: 0.130487\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.3%\n",
      "Minibatch loss at step 4950: 0.200502\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 5000: 1.108705\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 5050: 0.193768\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 5100: 0.331727\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 5150: 0.506960\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.8%\n",
      "Minibatch loss at step 5200: 0.232352\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.0%\n",
      "Minibatch loss at step 5250: 0.166380\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 5300: 0.238943\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 5350: 0.262512\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 5400: 0.392232\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 5450: 0.340364\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.4%\n",
      "Minibatch loss at step 5500: 0.508749\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 5550: 0.314524\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 5600: 0.337726\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.2%\n",
      "Minibatch loss at step 5650: 0.362860\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 5700: 0.528054\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 5750: 0.688255\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 5800: 0.236591\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 5850: 0.848808\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 5900: 0.851167\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.4%\n",
      "Minibatch loss at step 5950: 0.383159\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 6000: 0.145510\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 6050: 0.451479\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.1%\n",
      "Minibatch loss at step 6100: 0.944734\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.9%\n",
      "Minibatch loss at step 6150: 0.221575\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.1%\n",
      "Minibatch loss at step 6200: 1.130109\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 6250: 1.035455\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 6300: 0.753187\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 6350: 0.124487\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 6400: 0.110366\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 86.4%\n",
      "Minibatch loss at step 6450: 0.284560\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.9%\n",
      "Minibatch loss at step 6500: 0.808230\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 6550: 0.231092\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.5%\n",
      "Minibatch loss at step 6600: 0.429626\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 6650: 1.389380\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 6700: 0.215954\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 6750: 0.358699\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 6800: 0.671309\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 6850: 0.577780\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 6900: 0.606071\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 6950: 0.245747\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 7000: 1.043532\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 7050: 0.588938\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 7100: 0.487920\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 7150: 0.298476\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 7200: 0.595083\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 86.7%\n",
      "Minibatch loss at step 7250: 0.335184\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 7300: 0.506776\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 7350: 0.130336\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 7400: 0.013683\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 7450: 0.261001\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 86.8%\n",
      "Minibatch loss at step 7500: 0.222963\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.1%\n",
      "Minibatch loss at step 7550: 0.423917\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 7600: 0.540183\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 7650: 0.277236\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 7700: 0.080460\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 7750: 0.312135\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 7800: 0.324868\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 7850: 0.473891\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 7900: 0.107280\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 7950: 0.928129\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 8000: 0.411603\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 8050: 0.273585\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 8100: 0.518977\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 8150: 0.649213\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 8200: 0.326911\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 8250: 0.235424\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 8300: 0.413041\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 8350: 0.404637\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 8400: 0.376382\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 8450: 0.145150\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 8500: 0.325861\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 8550: 0.390560\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 8600: 0.380062\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 8650: 0.596368\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 8700: 0.269267\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 8750: 0.203600\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 8800: 0.183450\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 8850: 0.050129\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 8900: 0.499641\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.1%\n",
      "Minibatch loss at step 8950: 0.268172\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 9000: 0.307033\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 9050: 0.423569\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.0%\n",
      "Minibatch loss at step 9100: 0.327092\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 9150: 0.758888\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 86.6%\n",
      "Minibatch loss at step 9200: 0.322718\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 9250: 0.817988\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 9300: 1.074102\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 9350: 0.227235\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 9400: 0.228507\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 9450: 0.262066\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 9500: 0.168588\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 9550: 0.233744\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 9600: 0.308239\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 9650: 0.370479\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.2%\n",
      "Minibatch loss at step 9700: 0.226831\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 9750: 0.111030\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 9800: 0.546383\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 9850: 0.299850\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 9900: 0.652621\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 9950: 0.319600\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 10000: 0.171418\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 10050: 0.092044\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 10100: 0.364689\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 10150: 0.664580\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 10200: 0.145718\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 10250: 0.335909\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 10300: 0.120575\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 10350: 0.334384\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 10400: 0.385033\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 10450: 0.291096\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 10500: 0.499867\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 10550: 0.699872\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 10600: 0.694923\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 10650: 0.358271\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 10700: 0.038611\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 10750: 0.210932\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 10800: 0.343698\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 10850: 0.915179\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 10900: 0.182779\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 10950: 0.480516\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 11000: 0.133577\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 11050: 0.445598\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 11100: 0.116119\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 11150: 0.351546\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 11200: 0.214361\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 11250: 1.132205\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 11300: 0.416074\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 11350: 0.346354\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 11400: 0.207315\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 11450: 0.372729\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 11500: 0.423345\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 11550: 0.366858\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 11600: 0.413811\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 11650: 0.339718\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 11700: 1.128059\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 11750: 0.375930\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 11800: 0.018534\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 11850: 0.640144\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.5%\n",
      "Minibatch loss at step 11900: 0.278608\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 11950: 0.778321\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 12000: 0.537164\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 12050: 0.041181\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 12100: 0.359138\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 12150: 0.216178\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 12200: 0.431664\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 12250: 0.396865\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 12300: 0.332852\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 12350: 0.661470\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 12400: 0.024100\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 12450: 0.882016\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 12500: 0.666356\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 12550: 0.634275\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.3%\n",
      "Minibatch loss at step 12600: 0.504009\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 12650: 0.595563\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.8%\n",
      "Minibatch loss at step 12700: 0.481609\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 12750: 0.179394\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 12800: 0.152290\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 12850: 0.255823\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.7%\n",
      "Minibatch loss at step 12900: 0.294143\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 12950: 0.057567\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 13000: 0.243102\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 13050: 0.180230\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 13100: 0.232316\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 13150: 0.253453\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 13200: 0.243009\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 13250: 0.760742\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 13300: 0.293977\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 13350: 0.079770\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 13400: 0.462399\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 87.9%\n",
      "Minibatch loss at step 13450: 0.306897\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 13500: 0.291529\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 13550: 0.369528\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 13600: 0.257500\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 13650: 0.558677\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 13700: 0.374546\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 13750: 0.776668\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 13800: 0.059802\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 13850: 0.135528\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 13900: 0.236464\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 13950: 0.376923\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 14000: 0.054550\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 14050: 0.354875\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 87.4%\n",
      "Minibatch loss at step 14100: 0.535367\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 14150: 0.286994\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 14200: 0.264117\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.1%\n",
      "Minibatch loss at step 14250: 0.285940\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 14300: 0.236946\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 14350: 0.111533\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 14400: 0.355326\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 87.6%\n",
      "Minibatch loss at step 14450: 0.166920\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 14500: 0.447961\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 14550: 0.436902\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 14600: 0.026376\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 14650: 0.547449\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.0%\n",
      "Minibatch loss at step 14700: 0.194658\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 14750: 0.302396\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 14800: 0.473613\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 14850: 0.375199\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 14900: 0.294695\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 14950: 0.555640\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 15000: 0.288965\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 15050: 0.586662\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 15100: 0.165074\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 15150: 0.241651\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 15200: 0.070328\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 15250: 0.618133\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 15300: 0.195417\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 15350: 0.049322\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 15400: 0.903915\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 15450: 0.660888\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 15500: 0.122664\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 15550: 0.272184\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 15600: 0.363564\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 15650: 0.405549\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 15700: 0.256486\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 15750: 0.154002\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 15800: 0.239925\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 15850: 0.227647\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 15900: 0.555887\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 15950: 0.151216\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 16000: 0.816181\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 16050: 0.050174\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 16100: 0.273636\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 16150: 0.049309\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 16200: 0.695441\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 16250: 0.192707\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 16300: 0.550101\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 16350: 0.517190\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 16400: 0.485441\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 16450: 0.674784\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 16500: 0.014731\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 16550: 0.671563\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 16600: 0.300188\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 16650: 0.740503\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 16700: 0.382607\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.3%\n",
      "Minibatch loss at step 16750: 0.557894\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 16800: 0.890328\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 16850: 0.679777\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 16900: 0.922036\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 16950: 0.286368\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 17000: 0.221937\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 17050: 0.412557\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 17100: 0.444059\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 17150: 0.234638\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 17200: 0.278832\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 17250: 0.416110\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 17300: 0.239618\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 17350: 0.263696\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 17400: 0.425588\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 17450: 0.248632\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 17500: 0.430189\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 17550: 0.629030\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 17600: 0.781714\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 17650: 0.115577\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.2%\n",
      "Minibatch loss at step 17700: 0.522619\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.6%\n",
      "Minibatch loss at step 17750: 0.315973\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 17800: 0.513727\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 17850: 0.430481\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 17900: 0.474613\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 17950: 0.428169\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.4%\n",
      "Minibatch loss at step 18000: 0.515662\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 18050: 0.320211\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 18100: 0.546734\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 18150: 0.181387\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18200: 0.441694\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 18250: 0.710948\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18300: 0.114027\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 18350: 0.295958\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18400: 0.540027\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.5%\n",
      "Minibatch loss at step 18450: 0.264987\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18500: 0.223339\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 18550: 0.339755\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 18600: 0.106863\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 18650: 0.314317\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 18700: 0.219302\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 18750: 0.646007\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18800: 0.459860\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 88.9%\n",
      "Minibatch loss at step 18850: 0.586212\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 89.2%\n",
      "Minibatch loss at step 18900: 0.029771\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 18950: 0.105667\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 19000: 0.409830\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.7%\n",
      "Minibatch loss at step 19050: 0.201097\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.2%\n",
      "Minibatch loss at step 19100: 0.038737\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19150: 0.577870\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19200: 0.268420\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19250: 0.858772\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19300: 0.182273\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19350: 0.188322\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 19400: 0.576385\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19450: 0.195962\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 19500: 0.429300\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19550: 0.341993\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 19600: 0.384213\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.2%\n",
      "Minibatch loss at step 19650: 0.287310\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.2%\n",
      "Minibatch loss at step 19700: 0.124643\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 19750: 0.698890\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19800: 0.134224\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.4%\n",
      "Minibatch loss at step 19850: 0.197221\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 88.8%\n",
      "Minibatch loss at step 19900: 0.168109\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 89.1%\n",
      "Minibatch loss at step 19950: 0.691112\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 89.0%\n",
      "Minibatch loss at step 20000: 0.263018\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 89.4%\n",
      "Test accuracy: 94.7%\n"
     ]
    }
   ],
   "source": [
    "num_steps = 20001\n",
    "\n",
    "with tf.Session(graph=graph) as session:\n",
    "  tf.initialize_all_variables().run()\n",
    "  print('Initialized')\n",
    "  for step in range(num_steps):\n",
    "    offset = (step * batch_size) % (train_labels.shape[0] - batch_size)\n",
    "    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]\n",
    "    batch_labels = train_labels[offset:(offset + batch_size), :]\n",
    "    feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}\n",
    "    _, l, predictions = session.run(\n",
    "      [optimizer, loss, train_prediction], feed_dict=feed_dict)\n",
    "    if (step % 50 == 0):\n",
    "      print('Minibatch loss at step %d: %f' % (step, l))\n",
    "      print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))\n",
    "      print('Validation accuracy: %.1f%%' % accuracy(\n",
    "        valid_prediction.eval(), valid_labels))\n",
    "  print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The accuracy is good, but not as good as the 3-layer network from the previous assignment.\n",
    "\n",
    "The next version of the net uses dropout and learning rate decay:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "batch_size = 16\n",
    "patch_size = 5\n",
    "depth = 16\n",
    "num_hidden = 64\n",
    "beta_regul = 1e-3\n",
    "drop_out = 0.5\n",
    "\n",
    "graph = tf.Graph()\n",
    "\n",
    "with graph.as_default():\n",
    "\n",
    "  # Input data.\n",
    "  tf_train_dataset = tf.placeholder(\n",
    "    tf.float32, shape=(batch_size, image_size, image_size, num_channels))\n",
    "  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))\n",
    "  tf_valid_dataset = tf.constant(valid_dataset)\n",
    "  tf_test_dataset = tf.constant(test_dataset)\n",
    "  global_step = tf.Variable(0)\n",
    "  \n",
    "  # Variables.\n",
    "  layer1_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, num_channels, depth], stddev=0.1))\n",
    "  layer1_biases = tf.Variable(tf.zeros([depth]))\n",
    "  layer2_weights = tf.Variable(tf.truncated_normal(\n",
    "      [patch_size, patch_size, depth, depth], stddev=0.1))\n",
    "  layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))\n",
    "  size3 = ((image_size - patch_size + 1) // 2 - patch_size + 1) // 2\n",
    "  layer3_weights = tf.Variable(tf.truncated_normal(\n",
    "      [size3 * size3 * depth, num_hidden], stddev=0.1))\n",
    "  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))\n",
    "  layer4_weights = tf.Variable(tf.truncated_normal(\n",
    "      [num_hidden, num_hidden], stddev=0.1))\n",
    "  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))\n",
    "  layer5_weights = tf.Variable(tf.truncated_normal(\n",
    "      [num_hidden, num_labels], stddev=0.1))\n",
    "  layer5_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))\n",
    "  \n",
    "  # Model.\n",
    "  def model(data, keep_prob):\n",
    "    # C1 input 28 x 28\n",
    "    conv1 = tf.nn.conv2d(data, layer1_weights, [1, 1, 1, 1], padding='VALID')\n",
    "    bias1 = tf.nn.relu(conv1 + layer1_biases)\n",
    "    # S2 input 24 x 24\n",
    "    pool2 = tf.nn.avg_pool(bias1, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')\n",
    "    # C3 input 12 x 12\n",
    "    conv3 = tf.nn.conv2d(pool2, layer2_weights, [1, 1, 1, 1], padding='VALID')\n",
    "    bias3 = tf.nn.relu(conv3 + layer2_biases)\n",
    "    # S4 input 8 x 8\n",
    "    pool4 = tf.nn.avg_pool(bias3, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')\n",
    "    # F5 input 4 x 4\n",
    "    shape = pool4.get_shape().as_list()\n",
    "    reshape = tf.reshape(pool4, [shape[0], shape[1] * shape[2] * shape[3]])\n",
    "    hidden5 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)\n",
    "    # F6\n",
    "    drop5 = tf.nn.dropout(hidden5, keep_prob)\n",
    "    hidden6 = tf.nn.relu(tf.matmul(hidden5, layer4_weights) + layer4_biases)\n",
    "    drop6 = tf.nn.dropout(hidden6, keep_prob)\n",
    "    return tf.matmul(drop6, layer5_weights) + layer5_biases\n",
    "  \n",
    "  # Training computation.\n",
    "  logits = model(tf_train_dataset, drop_out)\n",
    "  loss = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))\n",
    "    \n",
    "  # Optimizer.\n",
    "  learning_rate = tf.train.exponential_decay(0.05, global_step, 1000, 0.85, staircase=True)\n",
    "  optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)\n",
    "  \n",
    "  # Predictions for the training, validation, and test data.\n",
    "  train_prediction = tf.nn.softmax(logits)\n",
    "  valid_prediction = tf.nn.softmax(model(tf_valid_dataset, 1.0))\n",
    "  test_prediction = tf.nn.softmax(model(tf_test_dataset, 1.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized\n",
      "Minibatch loss at step 0: 3.096471\n",
      "Minibatch accuracy: 18.8%\n",
      "Validation accuracy: 10.0%\n",
      "Minibatch loss at step 50: 2.218386\n",
      "Minibatch accuracy: 18.8%\n",
      "Validation accuracy: 27.3%\n",
      "Minibatch loss at step 100: 1.838299\n",
      "Minibatch accuracy: 43.8%\n",
      "Validation accuracy: 43.7%\n",
      "Minibatch loss at step 150: 1.187498\n",
      "Minibatch accuracy: 43.8%\n",
      "Validation accuracy: 61.2%\n",
      "Minibatch loss at step 200: 1.286091\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 65.4%\n",
      "Minibatch loss at step 250: 1.234681\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 68.3%\n",
      "Minibatch loss at step 300: 0.742321\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 72.1%\n",
      "Minibatch loss at step 350: 0.743286\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 69.9%\n",
      "Minibatch loss at step 400: 0.753913\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 75.8%\n",
      "Minibatch loss at step 450: 1.047853\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 72.6%\n",
      "Minibatch loss at step 500: 0.679798\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 76.4%\n",
      "Minibatch loss at step 550: 0.826316\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 75.3%\n",
      "Minibatch loss at step 600: 0.598534\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 77.4%\n",
      "Minibatch loss at step 650: 1.051054\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 77.1%\n",
      "Minibatch loss at step 700: 1.229106\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 78.8%\n",
      "Minibatch loss at step 750: 0.226812\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 76.7%\n",
      "Minibatch loss at step 800: 0.760882\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 78.7%\n",
      "Minibatch loss at step 850: 1.568783\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 78.3%\n",
      "Minibatch loss at step 900: 0.604009\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 79.3%\n",
      "Minibatch loss at step 950: 0.851479\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 78.8%\n",
      "Minibatch loss at step 1000: 0.622787\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 78.1%\n",
      "Minibatch loss at step 1050: 0.705176\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 78.4%\n",
      "Minibatch loss at step 1100: 0.786113\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 79.8%\n",
      "Minibatch loss at step 1150: 0.401123\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 80.6%\n",
      "Minibatch loss at step 1200: 0.952180\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 80.2%\n",
      "Minibatch loss at step 1250: 0.677300\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 80.6%\n",
      "Minibatch loss at step 1300: 0.481008\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 80.8%\n",
      "Minibatch loss at step 1350: 1.209476\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 80.6%\n",
      "Minibatch loss at step 1400: 0.419186\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 80.5%\n",
      "Minibatch loss at step 1450: 0.380538\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 81.2%\n",
      "Minibatch loss at step 1500: 0.955075\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 1550: 1.085058\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 1600: 0.977800\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 1650: 0.921607\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 81.0%\n",
      "Minibatch loss at step 1700: 0.688754\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.6%\n",
      "Minibatch loss at step 1750: 0.692282\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 81.7%\n",
      "Minibatch loss at step 1800: 0.705485\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 81.5%\n",
      "Minibatch loss at step 1850: 1.197821\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 82.1%\n",
      "Minibatch loss at step 1900: 0.369234\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 81.5%\n",
      "Minibatch loss at step 1950: 0.735324\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 82.6%\n",
      "Minibatch loss at step 2000: 0.119179\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 81.8%\n",
      "Minibatch loss at step 2050: 1.021600\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 82.4%\n",
      "Minibatch loss at step 2100: 0.517658\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.2%\n",
      "Minibatch loss at step 2150: 0.827290\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 81.8%\n",
      "Minibatch loss at step 2200: 0.311569\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.2%\n",
      "Minibatch loss at step 2250: 0.782547\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 82.8%\n",
      "Minibatch loss at step 2300: 0.712941\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 82.3%\n",
      "Minibatch loss at step 2350: 0.595645\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 82.5%\n",
      "Minibatch loss at step 2400: 0.733667\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 82.9%\n",
      "Minibatch loss at step 2450: 0.706617\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.0%\n",
      "Minibatch loss at step 2500: 0.785494\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.1%\n",
      "Minibatch loss at step 2550: 0.802653\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.0%\n",
      "Minibatch loss at step 2600: 0.146725\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 83.4%\n",
      "Minibatch loss at step 2650: 0.778106\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.4%\n",
      "Minibatch loss at step 2700: 0.851263\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.4%\n",
      "Minibatch loss at step 2750: 1.249370\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 82.6%\n",
      "Minibatch loss at step 2800: 0.751888\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.6%\n",
      "Minibatch loss at step 2850: 0.334603\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.6%\n",
      "Minibatch loss at step 2900: 0.727808\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.6%\n",
      "Minibatch loss at step 2950: 0.433410\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 3000: 0.786957\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 3050: 0.647733\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 3100: 0.653776\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 3150: 1.022243\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 83.7%\n",
      "Minibatch loss at step 3200: 0.533052\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.0%\n",
      "Minibatch loss at step 3250: 0.564300\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.9%\n",
      "Minibatch loss at step 3300: 0.118282\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 84.1%\n",
      "Minibatch loss at step 3350: 0.339224\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.1%\n",
      "Minibatch loss at step 3400: 0.859578\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 3450: 0.599495\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.8%\n",
      "Minibatch loss at step 3500: 0.561725\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 83.9%\n",
      "Minibatch loss at step 3550: 0.318682\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 84.4%\n",
      "Minibatch loss at step 3600: 0.118864\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 3650: 0.874074\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 3700: 1.051456\n",
      "Minibatch accuracy: 56.2%\n",
      "Validation accuracy: 84.6%\n",
      "Minibatch loss at step 3750: 0.973502\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 3800: 0.047199\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 3850: 0.768790\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.8%\n",
      "Minibatch loss at step 3900: 0.724833\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.9%\n",
      "Minibatch loss at step 3950: 0.013041\n",
      "Minibatch accuracy: 100.0%\n",
      "Validation accuracy: 84.3%\n",
      "Minibatch loss at step 4000: 0.481803\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 84.5%\n",
      "Minibatch loss at step 4050: 0.744841\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 83.9%\n",
      "Minibatch loss at step 4100: 0.617877\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.8%\n",
      "Minibatch loss at step 4150: 1.141286\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 84.0%\n",
      "Minibatch loss at step 4200: 0.355831\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 4250: 0.769242\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 84.4%\n",
      "Minibatch loss at step 4300: 1.063414\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 84.9%\n",
      "Minibatch loss at step 4350: 0.340466\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 4400: 1.378393\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 84.8%\n",
      "Minibatch loss at step 4450: 0.581816\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 4500: 0.741992\n",
      "Minibatch accuracy: 75.0%\n",
      "Validation accuracy: 85.1%\n",
      "Minibatch loss at step 4550: 0.337504\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.2%\n",
      "Minibatch loss at step 4600: 0.342177\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.2%\n",
      "Minibatch loss at step 4650: 0.818812\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 84.4%\n",
      "Minibatch loss at step 4700: 0.676614\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.3%\n",
      "Minibatch loss at step 4750: 0.897268\n",
      "Minibatch accuracy: 68.8%\n",
      "Validation accuracy: 85.6%\n",
      "Minibatch loss at step 4800: 0.560772\n",
      "Minibatch accuracy: 81.2%\n",
      "Validation accuracy: 85.0%\n",
      "Minibatch loss at step 4850: 0.384257\n",
      "Minibatch accuracy: 87.5%\n",
      "Validation accuracy: 85.2%\n",
      "Minibatch loss at step 4900: 0.360459\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.5%\n",
      "Minibatch loss at step 4950: 0.243155\n",
      "Minibatch accuracy: 93.8%\n",
      "Validation accuracy: 85.1%\n",
      "Minibatch loss at step 5000: 1.119264\n",
      "Minibatch accuracy: 62.5%\n",
      "Validation accuracy: 85.0%\n",
      "Test accuracy: 91.4%\n"
     ]
    }
   ],
   "source": [
    "num_steps = 5001\n",
    "\n",
    "with tf.Session(graph=graph) as session:\n",
    "  tf.initialize_all_variables().run()\n",
    "  print('Initialized')\n",
    "  for step in range(num_steps):\n",
    "    offset = (step * batch_size) % (train_labels.shape[0] - batch_size)\n",
    "    batch_data = train_dataset[offset:(offset + batch_size), :, :, :]\n",
    "    batch_labels = train_labels[offset:(offset + batch_size), :]\n",
    "    feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}\n",
    "    _, l, predictions = session.run(\n",
    "      [optimizer, loss, train_prediction], feed_dict=feed_dict)\n",
    "    if (step % 50 == 0):\n",
    "      print('Minibatch loss at step %d: %f' % (step, l))\n",
    "      print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))\n",
    "      print('Validation accuracy: %.1f%%' % accuracy(\n",
    "        valid_prediction.eval(), valid_labels))\n",
    "  print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Well, the accuracy is worst. This net has many meta parameters and I don't feel comfortable in tuning them randomly. I should probably change the depth and make it different between the layers, since it looks like the increasing number of feature maps is a key design item.\n",
    "\n",
    "I will do so in a next version."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "default_view": {},
   "name": "4_convolutions.ipynb",
   "provenance": [],
   "version": "0.3.2",
   "views": {}
  },
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
